import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { GTextFormat } from "../../../common/GTextFormat";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { ImagesConst } from "../../resource/ImagesConst";
import { ObjCreate } from "../common/ObjCreate";
import { Language } from "../../../../extend/language/Language";
import { DailyItemConfig } from "../../resource/DailyItemConfig";
import { DailyRule } from "../../rules/DailyRule";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { FunctionButtonPanel } from "./FunctionButtonPanel";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { SQuestionPrepareStart } from "../../../../Message/Public/SQuestionPrepareStart";
import { SQuestionData } from "../../../../Message/Public/SQuestionData";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { QuestionDataTool } from "./QuestionDataTool";
import { QuestionDatePanel } from "./QuestionDatePanel";
import { EQuestionFuncCode } from "../../../../Message/Public/EQuestionFuncCode";
import { DataEvent } from "../../events/DataEvent";
type int = number;
//class QuestionListPanel
    
    export  class QuestionListPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.init();
            Dispatcher.addEventListener(EventName.FuncCodeChange, this.onFuncCodeChangeHandler);
            Dispatcher.addEventListener(EventName.AnswerQuestion, this.onAnswerQuestionHandler);
            return;
        }

        /* internal  */init(): void
        {
            this._titleTf = new GTextFormat("", 14, 16763955, null, null, null, null, null, null, null, null, null, 2);
            var loc1=ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB);
            loc1.width = 391;
            loc1.height = 395;
            addChild(loc1);
            var loc2=ObjCreate.createBg(15, 60, 360, 2, this, ImagesConst.SplitLine);
            var loc3=Language.getStringByParam(35604, DailyItemConfig.instance.getInfoByType(DailyRule.Question).time);
            this._qTitle = ObjCreate.createGTextField(loc3, 10, 20, 370, 60, this, this._titleTf);
            this._qTitle.wordWrap = true;
            this._qTitle.multiline = true;
            this._notOpen = new Object /* flash.display.Bitmap */();
            this._notOpen.x = 120;
            this._notOpen.y = 150;
            addChild(this._notOpen);
            LoaderHelp.addResCallBack("question", this.onResLoaded);
            this._downCountTxt = ObjCreate.createTextField("", 100, 280, 300, 20, this);
            this._markTxt = ObjCreate.createTextField("", 200, 80, 100, 30, this);
            this._functionBtnPanel = new FunctionButtonPanel();
            this._functionBtnPanel.x = 15;
            this._functionBtnPanel.y = 320;
            addChild(this._functionBtnPanel);
            return;
        }

        /* internal  */onResLoaded(): void
        {
            this._notOpen.bitmapData = GlobalClass.getBitmapData(ImagesConst.q_notOpen);
            return;
        }

        public updateQuestion(arg1: SQuestionPrepareStart=null, arg2: SQuestionData=null): void
        {
            if (this._secTimer == null) 
            {
                this._secTimer = new SecTimer();
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
                this._secTimer.addListener(TimerType.COMPLETE, this.onCompleteHandler);
            }
            if (arg1 == null && arg2 == null) 
            {
                this._qTitle.text = Language.getStringByParam(35604, DailyItemConfig.instance.getInfoByType(DailyRule.Question).time);
                addChild(this._notOpen);
                if (this._questionDataPanel && this._questionDataPanel.parent) 
                {
                    removeChild(this._questionDataPanel);
                }
                this._downCountTxt.htmlText = "";
                this._markTxt.htmlText = "";
                if (!QuestionDataTool.isFuncUse) 
                {
                    this._functionBtnPanel.setFuncBtnEnable(true);
                }
            }
            else if (arg1 == null) 
            {
                if (arg2 != null) 
                {
                    this._isPrepare = false;
                    this._sQuestionData = arg2;
                    this._qTitle.text = this._sQuestionData.num + ". " + this._sQuestionData.question.questionName;
                    this._markTxt.htmlText = "";
                    this.initQuestionData();
                }
            }
            else 
            {
                this._isPrepare = true;
                this._sQuestionPrepareStart = arg1;
                this.initQuestionPrepareStart();
            }
            this._functionBtnPanel.updateFunctionNum();
            this._qTitle.setTextFormat(this._titleTf);
            return;
        }

        /* internal  */initQuestionPrepareStart(): void
        {
            if (this._notOpen && this._notOpen.parent) 
            {
                removeChild(this._notOpen);
            }
            if (QuestionDataTool.leftTime >= 0) 
            {
                this._secTimer.reset();
                this._secTimer.repeatCount = QuestionDataTool.leftTime;
                this._secTimer.start();
            }
            return;
        }

        /* internal  */initQuestionData(): void
        {
            if (this._notOpen && this._notOpen.parent) 
            {
                removeChild(this._notOpen);
            }
            if (this._questionDataPanel == null) 
            {
                this._questionDataPanel = new QuestionDatePanel();
                this._questionDataPanel.x = 40;
                this._questionDataPanel.y = 110;
            }
            addChild(this._questionDataPanel);
            this._questionDataPanel.updateQuestion(this._sQuestionData);
            this._questionDataPanel.setCheckBoxEnable();
            this._questionDataPanel.setFuncCode(0);
            this._questionDataPanel.removeResult();
            if (!QuestionDataTool.isFuncUse) 
            {
                this._functionBtnPanel.setFuncBtnEnable(true);
            }
            this._secTimer.reset();
            this._secTimer.repeatCount = QuestionDataTool.leftTime;
            this._secTimer.start();
            this._downCountTxt.htmlText = Language.getStringByParam(35605, 10);
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            var loc1=arg1.repeatCount;
            if (this._isPrepare) 
            {
                this._qTitle.text = Language.getStringByParam(35606, arg1.repeatCount);
                this._qTitle.setTextFormat(this._titleTf);
            }
            else if (QuestionDataTool.answerState != 0) 
            {
                if (QuestionDataTool.answerState != 1) 
                {
                    if (QuestionDataTool.answerState == 2) 
                    {
                        this._downCountTxt.htmlText = Language.getStringByParam(35608, loc1);
                    }
                }
                else 
                {
                    if (loc1 == 1 && this._questionDataPanel.funcCode == EQuestionFuncCode._EQuestionFuncCodeRemainTwo && !QuestionDataTool.haveAnswer && this._questionDataPanel) 
                    {
                        this._questionDataPanel.wrongFunction();
                    }
                    this._downCountTxt.htmlText = Language.getStringByParam(35607, loc1);
                }
            }
            else 
            {
                this._downCountTxt.htmlText = Language.getStringByParam(35605, loc1);
            }
            return;
        }

        /* internal  */onCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            if (this._isPrepare) 
            {
                return;
            }
            if (QuestionDataTool.answerState == 1) 
            {
                if (QuestionDataTool.selectAnswer == "") 
                {
                    QuestionDataTool.setWrongAnswer();
                }
            }
            return;
        }

        public showAnswer(): void
        {
            if (this._questionDataPanel == null) 
            {
                return;
            }
            this._questionDataPanel.setCheckBoxEnable();
            this._questionDataPanel.showResult();
            this._functionBtnPanel.setFuncBtnEnable(false);
            if (this._secTimer == null) 
            {
                return;
            }
            this._secTimer.reset();
            this._secTimer.repeatCount = QuestionDataTool.leftTime;
            this._secTimer.start();
            this._downCountTxt.htmlText = Language.getStringByParam(35608, 3);
            return;
        }

        public canAnswer(): void
        {
            if (this._questionDataPanel == null) 
            {
                return;
            }
            this._questionDataPanel.setCheckBoxEnable();
            this._questionDataPanel.correctFunction();
            if (!QuestionDataTool.isFuncUse) 
            {
                this._functionBtnPanel.setFuncBtnEnable(true);
            }
            if (this._secTimer == null) 
            {
                return;
            }
            this._secTimer.reset();
            this._secTimer.repeatCount = QuestionDataTool.leftTime;
            this._secTimer.start();
            this._downCountTxt.htmlText = Language.getStringByParam(35607, 10);
            return;
        }

        public updateFunctionNum(arg1: Array<any>=null): void
        {
            this._functionBtnPanel.updateFunctionNum(arg1);
            return;
        }

        public updateMark(arg1: int, arg2: Boolean): void
        {
            if (QuestionDataTool.selectAnswer == "" || !(QuestionDataTool.answerState == 2)) 
            {
                this._markTxt.htmlText = "";
                return;
            }
            var loc1="";
            if (arg2) 
            {
                loc1 = "<font size=\'16\' color=\'#ff6600\'>" + arg1 / 2 + Language.getString(35609) + "<font color=\'#ffffff\'> Ｘ 2</font></font>";
            }
            else 
            {
                loc1 = "<font size=\'16\' color=\'#ff6600\'>" + arg1 + Language.getString(35609) + "</font>";
            }
            this._markTxt.htmlText = loc1;
            return;
        }

        /* internal  */onFuncCodeChangeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._questionDataPanel == null) 
            {
                return;
            }
            this._questionDataPanel.setFuncCode(loc1);
            this._functionBtnPanel.setFuncBtnEnable(false);
            QuestionDataTool.isFuncUse = true;
            return;
        }

        /* internal  */onAnswerQuestionHandler(arg1: DataEvent): void
        {
            this._functionBtnPanel.setFuncBtnEnable(false);
            QuestionDataTool.isFuncUse = true;
            return;
        }

        public stopTimer(): void
        {
            if (this._secTimer) 
            {
                this._secTimer.stop();
                this._secTimer.isDelete = false;
            }
            return;
        }

        /* internal */ /* var */_qTitle: Object /* flash.text.TextField */;

        /* internal */ /* var */_titleTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_sQuestionPrepareStart: SQuestionPrepareStart;

        /* internal */ /* var */_sQuestionData: SQuestionData;

        /* internal */ /* var */_notOpen: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_isPrepare: Boolean;

        /* internal */ /* var */_questionDataPanel: QuestionDatePanel;

        /* internal */ /* var */_functionBtnPanel: FunctionButtonPanel;

        /* internal */ /* var */_downCountTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_markTxt: Object /* flash.text.TextField */;
    }
